package com.halo.controller;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;  
import org.springframework.ui.ModelMap;  
import org.springframework.web.bind.annotation.PathVariable;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestMethod;  
import org.springframework.web.servlet.ModelAndView;

import com.halo.pojo.OnlineWord;
import com.halo.service.PoService; 
@Controller
public class PoController {
	public PoController() {} ;
	@Autowired
	private PoService poService;
	@RequestMapping(value = "/createWord/{user}", method = RequestMethod.GET)  
    public ModelAndView createWord(HttpServletRequest request,HttpServletResponse response,   
            @PathVariable("user") String user, ModelMap modelMap) throws Exception {  
        modelMap.put("loginUser", user);  
        return new ModelAndView("/createWord/createWord", modelMap);  
    }  


	@RequestMapping(value = "/createWord/save", method = RequestMethod.POST)  
    public ModelAndView save(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		poService.save(request,response);
        return null;  
    }  
	
	@RequestMapping(value = "/createWord/list", method = RequestMethod.GET)  
    public ModelAndView list(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		List<OnlineWord> wordList = poService.list();
		modelMap.put("wordList", wordList);
		return new ModelAndView("/createWord/wordList", modelMap);  
    }  
	
	@RequestMapping(value = "/createWord/openword", method = RequestMethod.GET)  
    public ModelAndView openword(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
	    String filename = request.getParameter("filename");
	    String subject = request.getParameter("subject");
		modelMap.put("filename", filename);
		modelMap.put("subject", subject);
		return new ModelAndView("/createWord/openWord", modelMap);  
    }  
	
	
	public static void main(String[] args) throws Exception {
		//requsetUrl("https://weblogin.sustc.edu.cn/cas/login?username=888888&password=123456");、
		HttpClientUtil httpClientUtil = new HttpClientUtil();  
		String charset = "utf-8"; 
		String uri = "https://weblogin.sustc.edu.cn/cas/login";  
        Map<String,String> createMap = new HashMap<String,String>();  
        createMap.put("username","88888888");  
        createMap.put("password","123456");  
        String httpresult = httpClientUtil.doPost(uri,createMap,charset); 
        System.out.println("result:"+httpresult);  
  
	}
	
	@RequestMapping(value = "/test/p", method = RequestMethod.GET)  
    public ModelAndView p(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		//modelMap.put("ihtml", "\"<html>ddddddddddddddddddddddddddddddddddddddddddd</html>\"");
		String filePath = "D:\\announcement1efdfe8a-32fc-47dd-ac65-e4d59d9f2b64.html";
		
		FileInputStream fileinputstream = new FileInputStream(filePath);
		int lenght = fileinputstream.available();
		byte bytes[] = new byte[lenght];
		fileinputstream.read(bytes);
		modelMap.put("ihtml", new String(bytes, "UTF-8"));
		fileinputstream.close();
		return new ModelAndView("/test/p", modelMap);  
    }  
	
}
